package com.srb.core.controller.admin;

import com.srb.common.exception.Assert;
import com.srb.common.result.R;
import com.srb.common.result.ResponseEnum;
import com.srb.core.pojo.entity.IntegralGrade;
import com.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *  swagger2的常见注解：
 *      @Api、@ApiOperation、@ApiParam
 * @author dapeng
 * @since 2021-08-08
 */
@Slf4j
@Api(tags = {"积分等级管理"})
//@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Autowired
    private IntegralGradeService integralGradeService;

    @ApiOperation(value = "积分等级列表")
    @GetMapping(value = {"/list"})
    public R listAll(){
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list",list).message("获取成功列表");
    }

    @ApiOperation(value = "逻辑删除某个积分等级",tags = {"逻辑删除"})
    @DeleteMapping(value = {"/remove/{id}"})
    public R removeById(@ApiParam(value = "数据id",required = true,example = "100") @PathVariable(value = "id") Long id){
        boolean flag = integralGradeService.removeById(id);
        if (flag){
           return R.ok().message("删除成功");
        }
        return R.error();
    }

    @ApiOperation(value = "新增积分等级")
    @PostMapping(value = {"/save"})
    public R save(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){
        /*if (integralGrade.getBorrowAmount() == null){
            //跑出去后会被统一异常处理器处理
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }*/
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean flag = integralGradeService.save(integralGrade);
        if (flag){
            return R.ok().message("新增成功");
        }
        return R.error();
    }

    @ApiOperation(value = "根据ID获取积分等级")
    @GetMapping(value = {"/get/{id}"})
    public R getById(@ApiParam(value = "数据ID",required = true)@PathVariable(value = "id") Long id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (null != integralGrade){
            return R.ok().data("record",integralGrade);
        }
        return R.error();
    }

    @ApiOperation(value = "更新积分等级")
    @PutMapping(value = {"/update"})
    public R update(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){
        boolean flag = integralGradeService.updateById(integralGrade);
        if (flag){
            return R.ok().message("更新成功");
        }
        return R.error();
    }
}

